package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.dao.InvoiceDao;
import com.baichang.taoshunda.entity.Invoice;
import com.baichang.taoshunda.service.InvoiceService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 2018/3/12.
 */
@Service
public class InvoiceServiceImpl implements InvoiceService {
    @Autowired
    private InvoiceDao invoiceDao;

    @Override
    public boolean addInvoice(String invoiceHead, String invoiceNumber, Integer invoiceType, Integer userId) {
        Invoice invoice = new Invoice();
        invoice.setUserId(userId);
        invoice.setInvoiceHead(invoiceHead);
        invoice.setInvoiceType(invoiceType.toString());
        invoice.setInvoiceNumber(invoiceNumber);
        return invoiceDao.save(invoice) > 0;
    }

    @Override
    public boolean updateInvoiceInfo(String invoiceHead, String invoiceNumber, Integer invoiceType, Integer userId, Integer invoiceId) throws ServiceException {
        Invoice invoice = invoiceDao.getEntityById(invoiceId);
        if (invoice == null) {
            throw new ServiceException("发票信息不存在");
        }
        return invoiceDao.updateInvoiceInfo(invoiceHead, invoiceNumber, invoiceType, invoiceId);
    }

    @Override
    public boolean deleteInvoice(Integer invoiceId) {
        return invoiceDao.deleteById(invoiceId) > 0;
    }

    @Override
    public List<Map<String, Object>> findInvoiceList(Integer userId) {
        List<Map<String, Object>> mapList = invoiceDao.findInvoiceList(userId);
        return mapList;
    }

    @Override
    public Map<String, Object> getInvoiceInfo(Integer userId) {
        Map<String, Object> map = new HashedMap();
        map.put("invoiceInfo", invoiceDao.getInvoiceInfo(userId));
        return map;
    }
}
